﻿using PortfolioFinalProject.DataModels;
using PortfolioFinalProject.Web.IDataAdapters.Adapters;
using PortfolioFinalProject.Web.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace PortfolioFinalProject.Web.Controllers
{
    [Authorize]
    public class SkillController : ApiController
    {
        IPortfolioAdapter _adapter = new PortfolioAdapter();

        //GET api/skill
        [AllowAnonymous]
        public IHttpActionResult GetAllSkills()
        {
            List<Skill> skills = _adapter.GetAllSkills();
            return Ok(skills);
        }
        //POST api/skill
        public IHttpActionResult PostNewSkill([FromBody] Skill skill)
        {
            if (User.IsInRole("Admin"))
            {
                skill = _adapter.AddSkill(skill);
                return Ok(skill);
            }
            return Unauthorized();
        }
        //PUT api/skill
        public IHttpActionResult PutSkill([FromBody] Skill skill)
        {
            if (User.IsInRole("Admin"))
            {
                skill = _adapter.UpdateSkill(skill);
                return Ok(skill);
            }
            return Unauthorized();
        }
        //DELETE api/skill/:id
        public IHttpActionResult DeleteSkill(int id)
        {
            if (User.IsInRole("Admin"))
            {
                _adapter.DeleteSkill(id);
                return Ok();
            }
            return Unauthorized();
        }
    }
}
